package com.lsmw.oss.service.impl;
/*
 *ClassName:OSSServiceImpl
 *UserName:二号男嘉宾
 *CreateTime:2020/12/23 15:21
 */

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.lsmw.oss.service.OSSService;
import com.lsmw.oss.utils.ConstantPropertiesUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@Service
public class OSSServiceImpl implements OSSService {

    @Override
    public String uploadFileAvatar(MultipartFile file) {
        OSS ossClient = new OSSClientBuilder().build(ConstantPropertiesUtils.END_POINT,ConstantPropertiesUtils.KEY_ID,ConstantPropertiesUtils.KEY_SECRET);
        // String datePath = new DateTime().toString("yyyy/MM/dd"); // 使用工具日期格式化
        String uid = UUID.randomUUID().toString().replaceAll("-","");
        String fileName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        try {
            ossClient.putObject(ConstantPropertiesUtils.BUCKET_NAME,
                    "lsmw/img/"+uid+fileName,
                    file.getInputStream());
            ossClient.shutdown();
            String url = "https://"+ConstantPropertiesUtils.BUCKET_NAME+"."+ConstantPropertiesUtils.END_POINT+"/lsmw/img/"+uid+fileName;
            return url;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
